package com.luminary.demo.authentication.core.security;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import com.luminary.demo.authentication.model.security.UserMeta;

/**
 * 
 * token中加入自定义信息
 * 
 * @author wulinfeng
 *
 */
public class JwtTokenEnhancer implements TokenEnhancer {

	@Override
	public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
		Map<String, Object> info = new HashMap<String, Object>();
		UserMeta user = (UserMeta) authentication.getUserAuthentication().getPrincipal();
		info.put("username", user.getUsername());
		info.put("extraInfo", user.getExtraInfo());
		
		((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
		return accessToken;
	}

}
